package com.wuyoucar.controller;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TUser;
import com.wuyoucar.mapper.TReportMapper;
import com.wuyoucar.service.InTReportService;
import com.wuyoucar.service.InTUserService;
import com.wuyoucar.util.MD5;
import com.wuyoucar.util.PhoneCode;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/tuser")
public class TUserController {
    @Autowired
    InTUserService tUserService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    InTReportService tReportService;
    //展示用户的个人信息
    @GetMapping("/showinfo/{userid}")
    public ResultBean queryOneById(@PathVariable("userid") Integer userid){
        return tUserService.queryOneById(userid);
    }
    //展示用户的个人信息
    @GetMapping("/userinfo/{userid}")
    public ResultBean selectuserinfoById(@PathVariable("userid") Integer userid){
        return tUserService.selectuserinfoById(userid);
    }

    //修改用户的个人信息
    @PostMapping("/updateinfo")
    public ResultBean updateUserById(TUser user){
        return tUserService.updateUserById(user);
    }

    //发送验证码
    @GetMapping("/getvcode/{newphone}")
    public ResultBean getVcode(@PathVariable("newphone")String newphone){
        if ("".equals(newphone)){
            return new ResultBean(404,"手机号不能为空");
        }else{
            String vcode = PhoneCode.vcode();
            PhoneCode.getPhonemsg(newphone,vcode);
            stringRedisTemplate.opsForValue().set("vcode",vcode,300,TimeUnit.SECONDS);
            return new ResultBean(200,"发送成功");
        }

    }

    //修改手机号
    @GetMapping("/updateTel/{userid}/{newphone}/{vcode}")
    public ResultBean updateTel(@PathVariable("userid")Integer userid,
                                @PathVariable("newphone")String newphone,
                                @PathVariable("vcode")String vcode){
        return tUserService.updateTel(userid,newphone,vcode);
    }
    //设置密码
    @GetMapping("/setpwd/{password}/{userid}")
    public ResultBean setpwd(@PathVariable("password")String password,
                              @PathVariable("userid")Integer userid ){
        return tUserService.setpwd(MD5.MD5(password),userid);
    }
    //修改密码
    @GetMapping("/updatepwd/{password}/{oldpwd}/{userid}")
    public ResultBean updatepwd(@PathVariable("password")String password,
                                @PathVariable("oldpwd")String oldpwd,
                                @PathVariable("userid")Integer userid){
        Object data = tUserService.queryOneById(userid).getData();
        TUser tUser = (TUser) data;
        if(tUser.getPassword().equals(MD5.MD5(oldpwd))){
            if (tUser.getPassword().equals(MD5.MD5(password))){
                return new ResultBean(300,"新密码与原密码不能相同");
            }else{
                return tUserService.updatepwd(MD5.MD5(password),userid);
            }
        }else{
            return new ResultBean(400,"原密码错误,请重新输入");
        }
    }

    //修改或设置用户名
    @GetMapping("/updateUsername/{userid}/{username}")
    public ResultBean updateUsername(@PathVariable("userid")Integer userid,
                                     @PathVariable("username")String username){

        return tUserService.updateUsername(userid,username);
    }
    //查询理赔单
    @GetMapping("/mycliam/{userid}")
    public ResultBean selectreport(@PathVariable("userid")Integer userid){
        return tReportService.selectAll(userid);
    }
}
